<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
	<style type="text/css">
		body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
	</style>
	<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=aReVPjsTHg0TWBK6C5kzsEx7zmnxaZh9"></script>
	<script type="text/javascript" src="node_modules/file-saver/src/FileSaver.js" ></script>
	<script src="https://unpkg.com/gcoord@0.2.2/dist/gcoord.js"></script>
	<script type="text/javascript" src="jquery.min.js"></script>
	<title>添加行政区划</title>
</head>
<body>
	<div id="allmap"></div>
</body>
</html>

<script type="text/javascript">


 	var citys = [['04f4529505a74a45a1337e6092238edd','益阳'],
['096e68097e1b4d1e8b5d1046d08d132f','醴陵市'],
['09dd7d0626eb40b9ac4d2330cf154734','张家界'],
['0bca85255ac6401287f177d93431de3b','沅江市'],
['0e847b2f2b8c4d8887c9fde4a8181c78','汝城县'],
['1042bf435eb1424298567e084c28b449','花垣县'],
['130b6c4f483642459d691b7422969502','天心区'],
['16deb5682a204b59aa383cfee06674e2','雨花区'],
['183f03710188417f9a49a65b4fc2ed33','雁峰区'],
['18dd8baeb6584604bfdb4b7ee1de9947','常宁市'],
['1a353ff351374cd388506944a352b297','永州'],
['1b15d485816943918b1234186239034f','南岳区'],
['1d03b456ef0540508d2e501f4d635143','大祥区'],
['217f7f2da3eb4338b9c640eabba2b1a0','绥宁县'],
['21c430beae824eeb81e02f7c8dbc545e','长沙县'],
['223a8bccb6cd49378b1938f35ec6f8f5','道　县'],
['241e670d9dea4702bcddd728a71eb6c9','邵东县'],
['28c0c667720c4efa80d774c8f4627bf5','衡东县'],
['29387233283c4e7b857214003d166ebe','桑植县'],
['29f2c190bb8843b89b8e69a704df76e3','芦淞区'],
['2ab3b06f79774d78a42efc5a59a748c7','安仁县'],
['2b02e61125414389b6bdb9ce9a57bee7','古丈县'],
['2c2aa02fc6924d708f47333d832b121d','双清区'],
['2c31590761604d9bacdaf5828dfc3cd7','湘潭'],
['2e0d1afa382b4d27a3c4c24293f7e0e8','雨湖区'],
['2e40acd413fc4964af0b29133bea9934','靖州苗族侗族自治县'],
['2ffb5fb52916465697e794a2e306c86b','会同县'],
['3090f692d1d843b481817abdfdedfc8b','澧　县'],
['324261c7770f4d1fbddeb5b4e22cfe28','北湖区'],
['330d8344361f4145901048ff7c74bf0c','宁远县'],
['33936bf51080456b94fb1db023a181d3','辰溪县'],
['3a9fd1950c9c4d8ebf5c9ef8cf6b06ba','吉首市'],
['3abbc3c879f6467ea87383b6a7ea5429','芙蓉区'],
['3dbcfc2149c6407b85ce4be17f681d5e','宁乡市'],
['3e18a00056854407b9278c9a712f368c','芷江侗族自治县'],
['44310f272db3462b8c143cb5139112e4','祁东县'],
['4773798b47bd462c9643f926528a1679','耒阳市'],
['4d96bb1a20db4226941023ff423aeb0f','岳阳'],
['4f4be32db7f0498cbda7e7ff2c2127ae','衡南县'],
['50078c5cd7f14d3db95efc2f48fcd90b','东安县'],
['5180e1052bed42aa8b2981f1fd1884db','邵阳县'],
['534a5a52632f4a7e8886668cae5a084a','桃江县'],
['540ce806682d44d394c567c94953cc92','湘潭县'],
['5742c16d174c4dccb89a5fe9ead6b852','隆回县'],
['58873db036044036820b1689effe5ce5','韶山市'],
['5a08de467f9943498681c34eaf21ff3d','永兴县'],
['5a8c4f9a85444ce7a6a981b9b55d6239','岳塘区'],
['5a98d40bdc514f18a8b3938705ade832','攸　县'],
['5b811faf81f440a5bde5fe33edfe8f9b','常德'],
['6001e27f53d0418a99ac3d32b55771c1','双峰县'],
['618e366cadee4a2f949c83ad06742525','新邵县'],
['6440a467aaa543e38f2d0079d5540c58','桃源县'],
['64afb52658574c0bb31f8ac686dcb07a','浏阳市'],
['67a8388cfc984a80a5e754bb3e87d3e5','桂阳县'],
['6fb97efd218b4bafa3a212b9491fed40','江永县'],
['7113e89f0fc9457a8f77fcb823169ded','赫山区'],
['735360c807664f88a41883ed57424032','慈利县'],
['749c0ad742584d25addf572530cf65d9','开福区'],
['7517824d6a064415a56a62fc85521769','泸溪县'],
['784b305a04df4089b0ea309c33721788','平江县'],
['7883f6c9fde340838653733db26531b7','武陵源区'],
['799beb942ff84a6f8be0ccb6207474b7','城步苗族自治县'],
['7a9a47e3c718464ab029419c271f535b','衡阳'],
['7e8684f7043543bebd1cb66702728f24','南　县'],
['8069a53ed27c40218959b398d5c91c01','凤凰县'],
['80c02f397d72407d87e661e41ec10f11','郴州'],
['80eff23b326a4d8381009bbb438c62d3','长沙'],
['8392918e37af4f43b2ce3619e04fab00','汨罗市'],
['890edec4e9da448a864064b9641c5693','冷水滩区'],
['8bde72109e7243678f4393b10a515ec6','望城区'],
['8e8e3b39a6bc4e01a87a854eceb8a5b2','龙山县'],
['91acf75e7f6c474389b3a61e5662844e','新田县'],
['9250b71f14bf47c49760e33cafc1346c','君山区'],
['937d2e1d64394bd88425331874907474','华容县'],
['93a63d5f56ae4ba5a34096f42535107c','北塔区'],
['93e7cf75908941a1839f47a5f4d92c8d','岳阳楼区'],
['96c872bf3f1b4aa18a9cbcbaf6c7d257','衡阳县'],
['9737c0804b9e4bf1add79579b95bb3a2','安乡县'],
['975186c4a7e1462c9882efcad40d7c4d','临武县'],
['98999a82ca2b45d59bf49be5b9e0633e','桂东县'],
['9abef5b44ce14488929a8a71dc3a34ff','溆浦县'],
['9af5c1c3ac7e4d56a97b58a9892cd020','苏仙区'],
['9b4851511e3b466fbc8262c27ba2da71','茶陵县'],
['9be5c5e30f914ca3886be25b6bfaedac','武陵区'],
['9e2e10e0fcd94f2ea2da2715ccb647a5','洞口县'],
['a0885209fb4d42c6966848daadac267f','石峰区'],
['a1e86f7f6c9540959150f7a255856d90','鼎城区'],
['a57d3a72c87e4730968f3cf5d289601e','天元区'],
['a6160a02ee35443cb464dfc4efad421f','株洲市'],
['a6d520a46fae440ca915966c1fc30413','永定区'],
['a9141d1b884c42f4adc36439db47305e','株洲'],
['aa601f5490004a30a264b0c6c0bef563','临湘市'],
['b2d28022df174c7d875a3c518129110a','湘乡市'],
['b6141d22afbb43929c2144cb8db28adc','蓝山县'],
['b896cddf0a1746699694aa7262a22a14','新化县'],
['b924f522b1c5452abd1dae0cb8e04e34','炎陵县'],
['ba315244ed6443e28be298a71e2320fc','汉寿县'],
['bf02ed0eda74402ead90483becf22283','祁阳县'],
['bf248629ec094f5a902691fbae9af1be','双牌县'],
['c027dd058867430bb99d95f710671e12','新晃侗族自治县'],
['c0c41a90c81941d495020b2e0bf1b127','娄底'],
['c0e43a3b2194422790623b3738585dc2','临澧县'],
['c11668c8b4764048a30c956dbf436fbe','荷塘区'],
['c11ce699fcb340a390a1cac4eb7fec4d','通道侗族自治县'],
['c14701639c14438daed218148270d2c2','资兴市'],
['c1f550ef95174a3f8c6da08ab5fab67f','鹤城区'],
['c26f0b7a761247d18f45754aeba29c2b','邵阳'],
['c943162925ae4f1682cb15ceb431460f','石门县'],
['c9628ce919844e47b92765f79253fb1f','岳阳县'],
['c9e2f73dd8ea4771a989b2d2bc45a744','湘阴县'],
['cd86ce1b79d64dc4aceec53eb2a9e6cb','怀化'],
['ce7e6a2fd41e414d8f881926c53f6ccc','安化县'],
['cf2cbf37d4b9436db6e244e9c7a27118','中方县'],
['d05bf94c4c9e4e749c035d4c10b611f6','永顺县'],
['d08cc70441fd4f1bbd87f752fb9535c4','沅陵县'],
['d1f6da6dcd2a43e5a99e0cbb5bc4ccaa','蒸湘区'],
['d2bdd0a8af5b4d85beea9b1de2803172','岳麓区'],
['d94a2d1062064302a7b6bf65bb6237c0','资阳区'],
['dbd5ceb35abd4f3488fb84c6e52b69f9','新宁县'],
['dc1ae95371d6497d801cf20a5b3f8dc9','江华瑶族自治县'],
['df2d21e5aa084a97ae6ceb87b84c31e8','湘西'],
['dff39d1c9135492f86004283f85dfd4a','娄星区'],
['e4c9c25fb9c8460495c10cbb8d6bea85','洪江市'],
['e605e7077e194d0ab665e84ddb54be59','涟源市'],
['e64bb99c83b946b0af2918b43b6b4620','石鼓区'],
['eb1d3e489b724a47980212f1d7566a07','津市市'],
['ed87a0ff57b545c4b75972669bcfbebf','嘉禾县'],
['ee37217afaf54f2a8c3c3d9140e9b6fc','零陵区'],
['f237b2ffef3d404da6b32e4e4958a8ac','保靖县'],
['f31483435b9d4febabfbe009a9d023ae','武冈市'],
['f4ec01434c114a29924b3d580792ae4a','云溪区'],
['f57f02f119be4480afa7a7d0dbaaec7c','衡山县'],
['f78f32ef24ca4827becf5a7bc2fa7358','冷水江市'],
['f8c9304fedc04ddcacb038842af9f0c9','麻阳苗族自治县'],
['fa7573b03ef8450c82587e6b6dcbd678','宜章县'],
['fb86bbc701864524885d92e61cd23147','珠晖区']];

citys.push(['3c95ffa0e07b41b6b264c2adf15ec3e5','湖南']);
citys.push(['fc7c80875bfe4af8b245b6e02d4b3bc0','中国']);
	// 百度地图API功能
	var map = new BMap.Map("allmap");
	map.centerAndZoom(new BMap.Point(116.403765, 39.914850), 5);
	map.enableScrollWheelZoom();
	var idx = -1;
	var datas = [];
	function getBoundary(){
		if(idx++ >= citys.length -1){
			var blob = new Blob([JSON.stringify(datas)], {type: "text/plain;charset=utf-8"});
 			saveAs(blob, "hunan.txt");
			return;
		}
		var bdary = new BMap.Boundary();
		var cityName = citys[idx][1].replace("　","");
		// cityName = '中国';
		bdary.get(cityName, function(rs){       //获取行政区域
			map.clearOverlays();        //清除地图覆盖物       
			var count = rs.boundaries.length; //行政区域的点有多少个
			if (count === 0) {
				console.log('未能获取当前输入行政区域:'+citys[idx][1]);
				init();
				return ;
			}
          	var pointArray = [];
          	var coordinates = [];
          	var data = {};
          	data['id'] = citys[idx][0];
          	data['city'] = citys[idx][1];
			for (var i = 0; i < count; i++) {
				var ply = new BMap.Polygon(rs.boundaries[i], {strokeWeight: 2, strokeColor: "#ff0000"}); //建立多边形覆盖物
				map.addOverlay(ply);  //添加覆盖物
				pointArray = pointArray.concat(ply.getPath());
				//var c = rs.boundaries[i].replace(/,|;/g,"");
				c = transform(rs.boundaries[i]);
				coordinates.push(c);
			}
			map.setViewport(pointArray);    //调整视野      
			data['coordinates'] = coordinates.join(";").replace(/ /g,",");
			data['shapeType'] = count > 1 ? 'multiPolygon':'polygon';
			data['extent'] = getCoordinatesExtent(strTransfTo2dArray(data['coordinates'].split(";").join(",")));
			getCityPosition(cityName, data);
		});   
	}

	function transform(coordinates){
		var arr = coordinates.split(";");
		var result = [];
		for(var i in arr){
			var c = arr[i].split(",");
			c = gcoord.transform(c, gcoord.BD09, gcoord.EPSG4326);
			result.push(c);
		}
		return result.join(" ").replace(/,/g," ");
	}

	function init(){
		setTimeout(function(){
			getBoundary();
		}, 500);
	}

	init();

	function addlabel() {
	    var pointArray = [
	      new BMap.Point(121.716076,23.703799),
	      new BMap.Point(112.121885,14.570616),
	      new BMap.Point(123.776573,25.695422)];
	    var optsArray = [{},{},{}];
	    var labelArray = [];
	    var contentArray = [
	      "台湾是中国的！",
	      "南海是中国的！",
	      "钓鱼岛是中国的！"];
	    for(var i = 0;i < pointArray.length; i++) {
	      optsArray[i].position = pointArray[i];
	      labelArray[i] = new BMap.Label(contentArray[i],optsArray[i]);
	      labelArray[i].setStyle({
			color : "red",
			fontSize : "12px",
				 height : "20px",
				 lineHeight : "20px",
				 fontFamily:"微软雅黑"
			 });
	      map.addOverlay(labelArray[i]);
	    }	  
	}

	function getCityPosition(cityName,data){
		$.ajax({
		   type: "get",
		   url: "http://api.map.baidu.com/place/v2/search",
		   data: "query="+cityName+"&region=湖南&output=json&ak=tRr4qk9CM853usbdtIUsU6y63DCgzjte",
		   dataType: "jsonp",
		   success: function(msg){
		   		var results = msg.results;
		   		if(results.length != 1){
		   			console.error("当前results返回结果条数不止一条:"+JSON.stringify(msg));
		   			return;
		   		}
		   		var location = msg.results[0].location;
		   		location = gcoord.transform([location.lng,location.lat], gcoord.BD09, gcoord.EPSG4326);
		   		data['x'] = location[0];
		   		data['y'] = location[1];
		   		datas.push(data);
				init();
		   }
		});
	}


	function getCoordinatesExtent(coordinates){
		var len = coordinates.length;
		var minx,miny,maxx,maxy;
		for(var i = 0 ;i < len;i++){
			var coordinate = coordinates[i]
			var x = coordinate[0];
			var y = coordinate[1];
			minx = minx == undefined || x < minx ? x : minx;
			maxx = maxx == undefined || x > maxx ? x : maxx;
			miny = miny == undefined || y < miny ? y : miny;
			maxy = maxy == undefined || y > maxy ? y : maxy;
		}
		return [minx,miny,maxx,maxy];
	}

	function strTransfTo2dArray(str,split){
		split = split || ",";
		var len = str.length;
		var arr = [];
		var lastIdx = str.lastIndexOf(split);
		for(var i = 0 ;i <= len;i++){
			var a = str.indexOf(split,i);
			var b = lastIdx == a ? len : str.indexOf(split,a+1);
			if(a * b < 0){
				break;
			}
			var item = str.substring(i,a);
			var item2 = str.substring(a+1,b);
			i = b;
			arr.push([parseFloat(item),parseFloat(item2)]);
		}
		return arr;
	}
</script>